package com.gxa.service.impl;

import com.gxa.mapper.*;
import com.gxa.pojo.*;
import com.gxa.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * (Goods)表服务实现类
 *
 * @author makejava
 * @since 2020-10-29 09:09:26
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired(required = false)
    private GoodsMapper goodsMapper;
    @Autowired(required = false)
    private CoditAttribMapper coditAttribMapper;
    @Autowired(required = false)
    private CoditInforMapper coditInforMapper;
    @Autowired(required = false)
    private CoditPageMapper coditPageMapper;
    @Autowired(required = false)
    private CostPriceMapper costPriceMapper;
    @Autowired(required = false)
    private CostSellingMapper costSellingMapper;
    @Autowired(required = false)
    private SupplierzMapper supplierzMapper;
    @Autowired(required = false)
    private IooMapper iooMapper;
    @Autowired(required = false)
    private StoreMapper storeMapper;
    /**
     * 通过ID查询单条数据
     *
     * @param goodsId 主键
     * @return 实例对象
     */
    public Goods queryById(Integer goodsId) {
        Goods goods=goodsMapper.queryById(goodsId);
        if (goods!=null){
            goods.setCoditAttrib(coditAttribMapper.queryById(goods.getCoditAttribId()));
            CoditInfor coditInfor=coditInforMapper.queryById(goods.getCoditInforId());
            coditInfor.setSupplierz(supplierzMapper.queryById(coditInfor.getSuppliersId()));
            goods.setCoditInfor(coditInfor);
            goods.setCoditPage(coditPageMapper.queryById(goods.getCoditPageId()));
            goods.setCostPrice(costPriceMapper.queryById(goods.getCostPriceId()));
            goods.setCostSelling(costSellingMapper.queryById(goods.getCostPriceGoodId()));
            goods.setBranch(storeMapper.selBreaChMag(goods.getBranchCode()));
            return goods;
        }else {
            return null;
        }
    }
    /**
     * 通过商品编码查询商品
     *
     * @param goodsCode 主键
     * @return 实例对象
     */
    @Override
    public Goods queryBlurryGoods(String goodsCode) {
        CoditInfor coditInfor= coditInforMapper.selectCoditInforByGoodsCode(goodsCode);
        Goods goods = goodsMapper.queryById(coditInfor.getGoodsId());
        if (goods.getCoditAttribId()!=0){
            CoditAttrib coditAttrib = coditAttribMapper.queryById(goods.getCoditAttribId());
            goods.setCoditAttrib(coditAttrib);
        }
        if (goods.getCoditPageId()!=0){
            CoditPage coditPage = coditPageMapper.queryById(goods.getCoditPageId());
            goods.setCoditPage(coditPage);
        }
        if (goods.getCostPriceGoodId()!=0){
            CostSelling costSelling = costSellingMapper.queryById(goods.getCostPriceGoodId());
            goods.setCostSelling(costSelling);
        }
        if (goods.getCostPriceId()!=0){
            CostPrice costPrice = costPriceMapper.queryById(goods.getCostPriceId());
            goods.setCostPrice(costPrice);
        }
        if (goods.getCoditInforId()!=0){
            CoditInfor coditInfor1 = coditInforMapper.queryById(goods.getCoditInforId());
            goods.setCoditInfor(coditInfor1);
        }
        if (goods.getBranchCode()!=0){
            Branch branch = storeMapper.selBreaChMag(goods.getBranchCode());
            goods.setBranch(branch);
        }
        return goods;
    }
    /**
     * 查询所有商品
     * @return
     */
    @Override
    public List<Goods> selectGoods() {
        int totalSize=goodsMapper.totalSizeGoods();
        if(totalSize!=0){
            List<Goods> goodsList = goodsMapper.queryAll();
            for (Goods goods : goodsList) {
                goods.setCoditAttrib(coditAttribMapper.queryById(goods.getCoditAttribId()));
                CoditInfor coditInfor=coditInforMapper.queryById(goods.getCoditInforId());
                coditInfor.setSupplierz(supplierzMapper.queryById(coditInfor.getSuppliersId()));
                goods.setCoditInfor(coditInfor);
                goods.setCoditPage(coditPageMapper.queryById(goods.getCoditPageId()));
                goods.setCostPrice(costPriceMapper.queryById(goods.getCostPriceId()));
                goods.setCostSelling(costSellingMapper.queryById(goods.getCostPriceGoodId()));
                goods.setBranch(storeMapper.selBreaChMag(goods.getBranchCode()));
            }
            return goodsList;
        }else {
            return null;
        }
    }
    /**
     * 根据主键id删除商品
     * @param goodsId
     * @return
     */
    @Override
    public int deleteGoodsByGoodsId(int goodsId) {
        return goodsMapper.deleteById(goodsId);
    }
    /**
     * 修改商品
     * @param goods
     * @return
     */
    @Override
    public int updateGoods(Goods goods) {
        CoditAttrib coditAttrib = goods.getCoditAttrib();
        CoditInfor coditInfor = goods.getCoditInfor();
        CostPrice costPrice = goods.getCostPrice();
        CostSelling costSelling = goods.getCostSelling();
        CoditPage coditPage = goods.getCoditPage();
        coditAttrib.setGoodsId(goods.getGoodsId());
        coditInfor.setGoodsId(goods.getGoodsId());
        costPrice.setGoodsId(goods.getGoodsId());
        costSelling.setGoodsId(goods.getGoodsId());
        coditPage.setGoodsId(goods.getGoodsId());
        coditAttribMapper.update(coditAttrib);
        coditInforMapper.update(coditInfor);
        costPriceMapper.update(costPrice);
        costSellingMapper.update(costSelling);
        coditPageMapper.update(coditPage);
        return goodsMapper.update(goods);
    }
}